package com.xipu.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xipu.yygh.enums.DictEnum;
import com.xipu.yygh.feign.service_cmn.CmnFeignClient;
import com.xipu.yygh.model.user.Patient;
import com.xipu.yygh.user.mapper.PatientMapper;
import com.xipu.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author gql
 * @since 2022-05-10
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    /**
     * 远程调用数据字典模块
     */
    @Autowired
    private CmnFeignClient cmnFeignClient;

    @Override
    public List<Patient> findAllPatient(Long userId) {
        List<Patient> patientList = this.baseMapper.selectList(Wrappers.lambdaQuery(Patient.class).eq(Patient::getUserId, userId));
        patientList.forEach(this::packagePatient);
        return patientList;
    }

    @Override
    public Patient getPatientId(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packagePatient(patient);
        return patient;
    }

    /**
     * 遍历封装-获取编号对应的名称
     * @param item 就诊人信息
     */
    private void packagePatient(Patient item) {
        // 1.根据编号获得省、市、区、联系人证件类型名称
        String province = cmnFeignClient.getName(item.getProvinceCode());
        String city = cmnFeignClient.getName(item.getCityCode());
        String district = cmnFeignClient.getName(item.getDistrictCode());
        String certificatesType = cmnFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), item.getCertificatesType());

        // 2.将省、市、区、联系人证件类型名称放入map集合
        item.getParam().put("province", province);
        item.getParam().put("city", city);
        item.getParam().put("district", district);
        item.getParam().put("certificatesType", certificatesType);
        item.getParam().put("fullAddress", province + city + district + item.getAddress());
    }
}
